package com.google.gwt.sample.contacts.client.editcontact;

import com.google.gwt.sample.contacts.client.action.GetContactAction;
import com.google.gwt.sample.contacts.client.action.GetContactResponse;
import com.google.gwt.sample.contacts.client.action.UpdateContactAction;
import com.google.gwt.sample.contacts.client.action.UpdateContactResponse;
import com.google.gwt.sample.contacts.client.contacts.ContactsPresenter;
import com.google.gwt.sample.contacts.client.editcontact.EditContactPresenter.EditContactBinder;
import com.google.gwt.sample.contacts.client.hascontent.HasContent;
import com.google.gwt.sample.contacts.shared.Contact;
import com.google.gwt.user.client.ui.HasText;
import com.google.inject.Inject;

import com.guit.client.GuitPresenter;
import com.guit.client.async.AbstractAsyncCallback;
import com.guit.client.binder.GuitBinder;
import com.guit.client.binder.ViewField;
import com.guit.client.binder.ViewHandler;
import com.guit.client.place.Place;

public class EditContactPresenter extends GuitPresenter<EditContactBinder> implements
    Place<String> {

    public static interface EditContactBinder extends GuitBinder<EditContactPresenter> {
    }
    
    @ViewField
    HasText firstName;
    
    @ViewField(name="firstName")
    HasFocus firstNameFocus;
    
    @ViewField
    HasText lastName;
    
    @ViewField
    HasText emailAddress;

    private Contact contact;
    private final HasContent hasContent;

    @Inject
    public EditContactPresenter(HasContent hasContent) {
        this.hasContent = hasContent;
        this.contact = new Contact();
    }

    @ViewHandler
    public void cancelButton$click() {
        placeManager.go(ContactsPresenter.class);
    }

    @ViewHandler
    public void saveButton$click() {
        contact.setFirstName(firstName.getText());
        contact.setLastName(lastName.getText());
        contact.setEmailAddress(emailAddress.getText());

        commandService.execute(new UpdateContactAction(contact), new AbstractAsyncCallback<UpdateContactResponse>() {
            @Override
            public void success(UpdateContactResponse result) {
                placeManager.go(ContactsPresenter.class);
            }
        });
    }

    @Override
    public void go(String id) {
        hasContent.setContent(getView());
        if (id != null) {
            commandService.execute(new GetContactAction(id), new AbstractAsyncCallback<GetContactResponse>() {
                @Override
                public void success(GetContactResponse result) {
                    contact = result.getContact();
                    firstName.setText(contact.getFirstName());
                    lastName.setText(contact.getLastName());
                    emailAddress.setText(contact.getEmailAddress());
                    firstNameFocus.setFocus(true);
                }
            });
        } else {
            firstName.setText(null);
            lastName.setText(null);
            emailAddress.setText(null);
            firstNameFocus.setFocus(true);
        }
    }
}
